//列表页操作按钮-详情
$('.show-body .tit-li6-p1').on('click', function () {
    var Detai = $(this).parent().siblings('.tit-li2');
    var DetaiBt = $(this).children('i');
    if (Detai.hasClass('tit-li2-add')) {
        Detai.removeClass('tit-li2-add');
    } else {
        $('.show-body .tit-li2').removeClass('tit-li2-add');
        Detai.addClass('tit-li2-add');
    }

    if (DetaiBt.hasClass('fa-caret-down')) {
        DetaiBt.removeClass('fa-caret-down').addClass('fa-caret-right');
    } else {
        $('.show-body .tit-li6-p1 i').removeClass('fa-caret-down').addClass('fa-caret-right');
        DetaiBt.addClass('fa-caret-down').removeClass('fa-caret-right');
    }
});

//开启关闭按钮
layui.use(['form'], function (index) {
    var form = layui.form;
    //监听指定开关
    form.on('switch(switchTest)', function (data) {
        if (data.elem.checked == true) {
            layer.msg(
                '已启用',
                {time: 1000,}
            );
        } else {
            layer.msg(
                '已停用',
                {time: 1000,}
            );
        }
    });
});

//添加套餐
$(document).on('click', '.power-pack-add', function () {
    $('.details-name input').val('');
    $('#rule_id').val(0);
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: "/admin/authgroup/getRuleAdd",
        data: {},
        success: function (res) {
            if (res.code == 1) {
                $('.power-tree').html(res.data);
                $('.power-pack-show').hide();
                $('.power-pack-details').show();
            } else {
                layer.msg(res.msg);
            }
        }
    });
});

//修改按钮
$(document).on('click', '.show-body .main .tit-li6-p2', function () {
    var that = $(this);
    var name = that.parent().siblings('.tit-li1').html();
    var id = that.parent().attr('data-id');
    $('.details-name input').val(name);
    var creatorid = parseInt(that.parent().attr('data-creatorid'));
    if (creatorid == 0) {
        $('.details-name input').attr('disabled', 'disabled');
    }
    $('#rule_id').val(id);
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: "/admin/authgroup/getRuleEdit",
        data: {id: id},
        success: function (res) {
            if (res.code == 1) {
                $('.power-tree').html(res.data);
                $('.power-pack-show').hide();
                $('.power-pack-details').show();
            } else {
                layer.msg(res.msg);
            }
        }
    });
});

//返回/保存
$('.details-return').on('click', function () {
    $('.power-pack-show').show();
    $('.power-pack-details').hide();
});

//保存按钮
$(document).on('click', '.details-save', function () {
    var that = $(this);
    var id = parseInt($('#rule_id').val());
    var name = $('.details-name input').val();
    //判断套餐名称是否为空
    if (name.length < 1) {
        layer.msg(
            '请输入套餐名称',
            {time: 2000,}
        );
    }
    var id_array = [];
    $('.power-tree-add').each(function () {
        id_array.push($(this).attr('data-id'));
    });
    var _data = {rules: id_array.join(','), title: name}
    if (id > 0) {
        var url = '/admin/authgroup/edit';
        _data.id = id;
    } else {
        var url = '/admin/authgroup/publish';
    }
    //判断分页列表长度是否大于分页20，如果不足20，则新增时则追加到管理员列表
    var ul_length = $('.main ul').length;
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: url,
        data: _data,
        success: function (res) {
            if (res.code == 1) {
                layer.msg(
                    '保存成功',
                    {time: 1000,}
                );
                if (id > 0) {
                    $("#ul_" + id).find('.tit-li1').html(res.data.title);
                    $("#ul_" + id).find('.tit-li2').html(res.data.rule_name);
                    $("#ul_" + id).find('.tit-li4').html(res.data.update_time);
                } else {
                    if (ul_length < 20) {
                        var html = '<ul>';
                        html += '<li class="tit-li1">' + res.data.title + '</li>';
                        html += '<li class="tit-li2">' + res.data.rule_name + '</li>';
                        html += '<li class="tit-li3">' + res.data.create_time + '</li>';
                        html += '<li class="tit-li4">' + res.data.update_time + '</li>';
                        html += '<li class="tit-li5">';
                        html += '   <form class="layui-form" action="">';
                        html += '       <div class="layui-form-item">';
                        html += '           <div class="layui-input-block">';
                        html += '               <input type="checkbox" name="yyy" lay-skin="switch" lay-text="启用|停用" checked lay-filter="switchTest" value="1">';
                        html += '           </div>';
                        html += '      </div>';
                        html += '   </form>';
                        html += '</li>';
                        html += '<li class="tit-li6" data-id="176" data-creatorid="2">';
                        html += '   <p class="tit-li6-p1"><i class="fa fa-caret-right"></i><span>详情</span></p>';
                        html += '   <p class="tit-li6-p2">修改</p>';
                        html += '   <p class="tit-li6-p3">删除</p>';
                        html += '</li>';
                        html += '</ul>';
                        $(".main").append(html);
                    }
                }
                $('.power-pack-show').show();
                $('.power-pack-details').hide();
            } else {
                layer.msg(res.msg);
            }
        }
    });

});

// 一级权限选中
$(document).on('click', '.level-one .tree-choose-one', function () {
    var One = $(this).parent().siblings().find('.tree-choose-one')
    if ($(this).hasClass('power-tree-add')) {
        $(this).removeClass('power-tree-add');
        One.removeClass('power-tree-add');
    } else {
        $(this).addClass('power-tree-add');
        One.addClass('power-tree-add');
    }
});

// 二级权限选中
$(document).on('click', '.level-two .tree-choose-one', function () {
    var Two = $(this).parent().siblings().find('.tree-choose-one');
    var PPtwo = $(this).parent().parent().siblings('.level-one').find('.tree-choose-one');
    if ($(this).hasClass('power-tree-add')) {
        $(this).removeClass('power-tree-add');
        Two.removeClass('power-tree-add');
    } else {
        $(this).addClass('power-tree-add');
        Two.addClass('power-tree-add');
        PPtwo.addClass('power-tree-add');
    }
});

// 三级选中
$(document).on('click', '.level-three .tree-choose-one', function () {
    var Ptwo = $(this).parent().parent().siblings().find('.tree-choose-one');
    var Pone = $(this).parent().parent().parent().siblings('.level-one').find('.tree-choose-one');
    if ($(this).hasClass('power-tree-add')) {
        $(this).removeClass('power-tree-add');
    } else {
        $(this).addClass('power-tree-add');
        Pone.addClass('power-tree-add');
        Ptwo.addClass('power-tree-add');
    }
});

// 展开与收起
$(document).on('click', '.power-tree .tree-down', function () {
    var DownUp = $(this).children();
    $(this).parent().siblings().toggle();
    if (DownUp.hasClass('fa-caret-down')) {
        DownUp.removeClass('fa-caret-down');
        DownUp.addClass('fa-caret-right');
    } else {
        DownUp.removeClass('fa-caret-right');
        DownUp.addClass('fa-caret-down');
    }
});